<?php
namespace app\zhonghang\controller;
use think\Db;
use think\Controller;
use think\Request;
use think\Session;
use think\Cache;
use think\Cookie;

class Goods extends Base{
 	public function _initialize(){
        parent::_initialize();
//        $where['id'] = array('neq',1);
//        $gc=M("Goods_category")->where($where)->order("sort asc")->select();
//        $this->assign("gc",$gc);

    }
	//空操作
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}


	//热销商品列表
	public function rexiaoGoods(){
        $list=DB::name('Goods')->where('is_hot','1')->select();
        if($list){
            $data['info']='全部热销商品';
            $data['status']='1';
            $data['data']=$list;
            return json($data);
        }else{
            $data['info']='暂无数据';
            $data['status']='-1';
            $data['data']=array();
            return json($data);
        }
    }

    //更多热销---商品列表
    public function goodsList()
    {
        $member = $this->member;
        $keywords = input('keywords');
        if(input('type_id')!=0){
            $where['cat_id'] = input('type_id');
        }
        if(trim(input('keywords'))!==''){
            $where['keywords'] = array('like','%'.trim(input('keywords')).'%');
        }
        $arr['keywords'] = trim(input('keywords'));
        $arr['add_time'] = time();
        $arr['user_id'] = $member['user_id'];
        $list = DB::name('Goods')->where($where)->select();
        $res = DB::name('search_word')->where('keywords',$keywords)->select();
        if($res == null)
        {
            $re = DB::name('search_word')->insert($arr);
        }else{
            $re = DB::name('search_word')->where('keywords',$keywords)->update($arr);
        }
        if ($list) {
            $data['info'] = '全部商品';
            $data['status'] = '1';
            $data['data'] = $list;
            return json($data);
        } else {
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    历史记录的显示
    public function history()
    {
        $member = $this->member;
        $res = DB::name('search_word')->where('user_id',$member['user_id'])->order('add_time desc')->select();
        if($res)
        {
            $data['info'] = '历史记录';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

//   清除历史记录
    public function delHistory()
    {
        $member = $this->member;
        $res = DB::name('search_word')->where('user_id',$member['user_id'])->delete();
        if($res)
        {
            $data['info'] = '清除历史记录成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info'] = '清除历史记录失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

//    按销量排序列表
    public function salesRanking()
    {
        $res = DB::name('goods')->order('sales_sum desc')->select();
        if($res)
        {
            $data['info'] = '销量排序';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '取值失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

//    按价格排序列表 价格由低到高
    public function priceRankingd()
    {
        $res = DB::name('goods')->order('shop_price asc')->select();
        if($res)
        {
            $data['info'] = '价格排序';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '取值失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
 //    按价格排序列表 价格由高到低
    public function priceRankingg()
    {
        $res = DB::name('Goods')->order('shop_price desc')->select();
        if($res)
        {
            $data['info'] = '价格排序';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '取值失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

//    商品详情
    public function goodsDetails()
    {
        $goods_id = input('goods_id');
        $res = DB::name('Goods')->where('goods_id',$goods_id)->find();
        $res['goods_pic']= DB::name('goods_images')->where('goods_id',$goods_id)->select();
        if($res)
        {
            $data['info'] = '商品详情';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    商品评价
    public function goodsEvaluate()
    {
        $goods_id = input('goods_id');
        $list = DB::name('comment')->where('goods_id',$goods_id)->find();
        if($list)
        {
            $data['info'] = '商品评价';
            $data['status'] = '1';
            $data['data'] = $list;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    添加评价
    public function addEvaluate()
    {
        $member = $this->member;
        $goods_id = input('goods_id');
        $arr['content'] = input('content');
        $arr['order_id'] = input('order_id');
        $arr['add_time'] = time();
        $arr['ip_address'] = $_SERVER["REMOTE_ADDR"];
        $image=input('img');
        $imageName = rand(1000,100000).time().'.png';
        if (strstr($image,",")){
            $image = explode(',',$image);
            $image = $image[1];
        }
        $path = "/public/uploads/real";
        $imageSrc= $path."/". $imageName; //图片名字
        $r = file_put_contents(ROOT_PATH.$imageSrc, base64_decode($image));
        $arr['img']=$imageSrc;
        $list = DB::name('comment')->insert($arr);
        if($list)
        {
            $data['info'] = '评价成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info'] = '评价失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }

//    待付款商品列表
    public function stayPayment()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('pay_status',0)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['goods_num'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_num');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '待付款商品';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    取消订单
    public function cancelOrder()
    {
        $member = $this->member;
        $order_id = input('order_id');
        $arr['order_status'] = '6';
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_id',$order_id)->update($arr);
        if($res)
        {
            $data['info'] = '取消订单成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info'] = '取消订单失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//   待发货商品列表
    public function stayGoods()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('shipping_status',0)->where('pay_status',1)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['goods_num'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_num');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '待发货商品';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    待收货商品列表
    public function stayReceiving()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('pay_status',1)->where('shipping_status',1)->where('order_status',0)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['goods_num'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_num');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '待收货商品';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    商品申请退款
    public function goodsRefund()
    {
        $member = $this->member;
        $order_id = input('order_id');
        $res = DB::name('Order_goods')->where('order_id',$order_id)->find();
        $order = DB::name('Order')->where('user_id',$member['user_id'])->where('order_id',$order_id)->find();
        $style = DB::name('Goods_stock')->where('goods_id',$res['goods_id'])->find();//商品属性库存
        $ar['order_status'] = '3';
        $arr['order_id'] = $order_id;// 订单id
        $arr['order_sn'] = $order['order_sn'];//订单号
        $arr['goods_id'] = $res['goods_id'];//商品id
        $arr['goods_num'] = $res['goods_num'];//退款商品数量
        $arr['user_id'] = $member['user_id'];//用户id
        $arr['type'] = '0';//退货
        $arr['addtime'] = time();//申请时间
        $arr['status'] = '0';//待审核
        $arr['refund_money'] = $order['order_amount'];//退款金额
//        $arr['refund_deposit'] = $order['order_amount'];//退款余额
        $arr['rec_id'] = $res['rec_id'];//order_goods表id
        $arr['reason'] = input('reason');//退货原因
        if($arr['reason'] == null)
        {
            $this->error('退款原因不能为空');
        }
        $image=input('zpic');
        $imageName = rand(1000,100000).time().'.png';
        if (strstr($image,",")){
            $image = explode(',',$image);
            $image = $image[1];
        }
        $path = "/public/uploads/real";
        $imageSrc= $path."/". $imageName; //图片名字
        $r = file_put_contents(ROOT_PATH.$imageSrc, base64_decode($image));
        $arr['imgs']=$imageSrc;
        $list = DB::name('Return_goods')->insert($arr);
        $li = DB::name('Order')->where('user_id',$member['user_id'])->where('order_id',$order_id)->update($ar);
        if($res && $list && $li)
        {
            $data['info'] = '申请退款';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '操作失误';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    撤销退款申请
    public function revokeApply()
    {
        $member = $this->member;
        $order_id = input('order_id');
        $arr['pay_status'] = '0';
        $arr['shipping_status'] = '0';
        $arr['order_status'] = '0';
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_id',$order_id)->update($arr);
        $ar['status'] = '-2';
        $list = DB::name('Return_goods')->where('order_id',$order_id)->update($ar);
        if($res && $list)
        {
            $data['info'] = '撤销申请成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info'] = '撤销申请失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    待评价商品列表
    public function stayEvaluate()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('pay_status',1)->where('shipping_status',1)->where('order_status',1)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['goods_num'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_num');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '待评价商品';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    全部订单列表
    public function stayWhole()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['goods_num'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_num');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '全部订单';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    退款
//      退款申请中列表
    public function refundApply()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_status',3)->where('pay_status',1)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['goods_num'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_num');
            $res[$k]['status'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('status');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '商品退款申请';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    退款申请完成列表
    public function refundComplete()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_status',4)->where('pay_status',1)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['goods_num'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_num');
            $res[$k]['status'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('status');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '商品退款完成';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    退款申请中详情
    public function refundDetails()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_status',3)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['reason'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('reason');
            $res[$k]['imgs'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('imgs');
            $res[$k]['addtime'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('addtime');
            $res[$k]['refund_money'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('refund_money');
            $res[$k]['status'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('status');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '申请退款详情';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    退款完成详情
    public function refundcompleteDetails()
    {
        $member = $this->member;
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_status',4)->select();
        foreach($res as $k=>$v)
        {
            $res[$k]['goods_name'] = DB::name('Order_goods')->where('order_id',$v['order_id'])->value('goods_name');
            $res[$k]['goods_id'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('goods_id');
            $res[$k]['reason'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('reason');
            $res[$k]['imgs'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('imgs');
            $res[$k]['addtime'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('addtime');
            $res[$k]['refund_money'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('refund_money');
            $res[$k]['status'] = DB::name('Return_goods')->where('order_id',$v['order_id'])->value('status');
        }
        foreach($res as $kk=>$vv)
        {
            $res[$kk]['color'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('color');
            $res[$kk]['label'] = DB::name('Goods_stock')->where('goods_id',$vv['goods_id'])->value('label');
        }
        if($res)
        {
            $data['info'] = '退款完成详情';
            $data['status'] = '1';
            $data['data'] = $res;
            return json($data);
        }else{
            $data['info'] = '暂无数据';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    删除订单
    public function delOrder()
    {
        $member = $this->member;
        $order_id = input('order_id');
        $arr['order_status'] = '5';
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_id',$order_id)->update($arr);
        if($res)
        {
            $data['info'] = '订单删除成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info'] = '订单删除失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
//    确认收货
    public function confirmGoods()
    {
        $member = $this->member;
        $order_id = input('order_id');
        $arr['order_status'] = '1';
        $res = DB::name('Order')->where('user_id',$member['user_id'])->where('order_id',$order_id)->update($arr);
        if($res)
        {
            $data['info'] = '确认收货成功';
            $data['status'] = '1';
            $data['data'] = array();
            return json($data);
        }else{
            $data['info'] = '确认收货失败';
            $data['status'] = '-1';
            $data['data'] = array();
            return json($data);
        }
    }
}